<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Exceptions;

use Exception;
use App\Mail\ExceptionReport;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\Debug\Exception\FlattenException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     */
    public function report(\Throwable $e)
    {
        if (config('mail.mailException.report') && $this->shouldReport($e)) {
            $this->mailReport($e, config('mail.mailException.toAccounts', []));
        }
        parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function render($request, \Throwable $e)
    {
        return parent::render($request, $e);
    }

    /**
     * 邮件通知错误报告.
     *
     * @param $exception object 错误信息
     * @param array $accounts array 收件人
     */
    public function mailReport($exception, $accounts = [])
    {
        if (!empty($accounts)) {
            try {
                $e       = FlattenException::create($exception);
                $handler = new SymfonyExceptionHandler();
                $html    = $handler->getHtml($e);
                $mail    = new Mail();
                $mail::to($accounts)->send(new ExceptionReport($html));
            } catch (Exception $ex) {
            }
        }
    }
}
